﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using RMS.Model;
using RMS.View;
using System.Windows.Forms;

namespace RMS.Controller
{
    public class AcademicProgramController
    {   
        private AcademicProgram _model;
        private AcademicProgramView _view;
        private List<AcademicProgram> _programList;

        public AcademicProgramController(AcademicProgram model, AcademicProgramView view)
        {
            this._model = model;
            this._view = view;
            this._programList = new List<AcademicProgram>();
            // observers ?..?
        }

        public AcademicProgram Model
        {
            get { return this._model; }
            set { this._model = value; }
        }
        public AcademicProgramView View
        {
            get { return this._view; }
            set { this._view = value; }
        }
        public List<AcademicProgram> ProgramList
        {
            get { return this._programList; }
            set { this._programList = value; }
        }
      /*  
        public void AddDiscipline(Discipline discipline)
        {
            if (discipline == null) return;
            this._model.DisciplineList.Add(discipline);
        }
                
        public bool RemoveDiscipline(Discipline discipline)
        {
            if (discipline == null) return false;
            return this._model.DisciplineList.Remove(discipline);
        }
                
        public bool UpdateDiscipline(Discipline discipline)
        {
            if (discipline == null) return false;
            foreach (Discipline currentDiscipline in this._model.DisciplineList)
            {
                if (currentDiscipline.Equals(discipline))
                {
                    currentDiscipline.Name = discipline.Name;
                    currentDiscipline.Semester = discipline.Semester;
                    return true;
                }
            }
            return false;
        }
                
        public Discipline SearchDiscipline(string disciplineName)
        {
            foreach (Discipline currentDiscipline in this._model.DisciplineList)
            {
                if (currentDiscipline.Name == disciplineName)
                    return currentDiscipline;
            }
            return null;
        }

        public Discipline SearchDiscipline(int disciplineId)
        {
            foreach (Discipline currentDiscipline in this._model.DisciplineList)
            {
                if (currentDiscipline.Id == disciplineId)
                    return currentDiscipline;
            }
            return null;
        } */
        /*
        public void SyncAcademicProgram(int programId)
        {
            this._model.DisciplineList = Persistence.DataManager.Instance.SyncDisciplineListForProgram(programId);
        } */

        public void AddProgram(AcademicProgram program)
        {
            if (program == null) return;
            int programId = 0;
            if ((programId = Persistence.DataManager.Instance.InsertAcademicProgram(program)) > 0)
            {
                program.Id = programId;
                this._programList.Add(program);
                this._view.PopulateProgramList(this._programList);
            }
        }

        public bool RemoveProgram(AcademicProgram program)
        {
            if (program == null) return false;
            bool found = this._programList.Remove(program);
            if (found)
            {
                Persistence.DataManager.Instance.RemoveAcademicProgram(program);
                this._view.PopulateProgramList(this._programList);
            }
            return found;
        }

        public bool UpdateProgram(AcademicProgram program)
        {
            if (program == null) return false;
            foreach (AcademicProgram currentProgram in this._programList)
            {
                if (currentProgram.Id == program.Id)
                {
                    currentProgram.Name = program.Name;
                    currentProgram.Description = program.Description;
                    currentProgram.SemesterCount = program.SemesterCount;
                    currentProgram.Type = program.Type;
                    Persistence.DataManager.Instance.UpdateAcademicProgram(program);
                    return true;
                }
            }
            return false;
        }

        public List<AcademicProgram> SearchProgram(string name)
        {
            List<AcademicProgram> list = new List<AcademicProgram>();
            foreach (AcademicProgram program in this._programList)
            {
                if (program.Name == name)
                    list.Add(program);
            }
            return list;
        }

        public bool ValidateInput()
        {
            if (this._view.TextBoxName == "")
            {
#if DEBUG
                throw new ArgumentNullException();
#else
                MessageBox.Show("empty name");
                return false;
#endif
            }
            if (this._view.TextBoxSemesters == "")
            {
#if DEBUG
                throw new ArgumentNullException();
#else
                MessageBox.Show("empty semester count");
                return false;
#endif
            }
            if (this._view.ComboBoxType == "")
            {
#if DEBUG
                throw new ArgumentNullException();
#else
                MessageBox.Show("empty type");
                return false;
#endif
            }
            
            return true;
        }

        public void HandleAddButtonClick()
        {
            if (!ValidateInput()) return;
            AcademicProgramType type;
            switch (this._view.ComboBoxType)
            {
                case "Undergrad":
                    type = AcademicProgramType.Undergrad;
                    break;
                case "Master":
                    type = AcademicProgramType.Master;
                    break;
                default:
#if DEBUG
                    throw new ArgumentOutOfRangeException();
#else
                    MessageBox.Show("wrong type");
                    return;
#endif                    
            }
            AcademicProgram program = new AcademicProgram(Convert.ToInt32(this._view.TextBoxId), this._view.TextBoxName, Convert.ToInt32(this._view.TextBoxSemesters), type, this._view.TextBoxDescription);
            this.AddProgram(program);
        }

        public void HandleRemoveButtonClick()
        {
            AcademicProgram program = new AcademicProgram(Convert.ToInt32(this._view.TextBoxId), "", 0, AcademicProgramType.Master);
            this.RemoveProgram(program);
        }

        public void HandleUpdateButtonClick()
        {
            if (!ValidateInput()) return;
            AcademicProgramType type;
            switch (this._view.ComboBoxType)
            {
                case "Undergrad":
                    type = AcademicProgramType.Undergrad;
                    break;
                case "Master":
                    type = AcademicProgramType.Master;
                    break;
                default:
#if DEBUG
                    throw new ArgumentOutOfRangeException();
#else
                    MessageBox.Show("wrong type");
                    return;
#endif
            }
            AcademicProgram program = new AcademicProgram(Convert.ToInt32(this._view.TextBoxId), this._view.TextBoxName, Convert.ToInt32(this._view.TextBoxSemesters), type, this._view.TextBoxDescription);
            this.UpdateProgram(program);
        }

        public void HandleSearchButtonClick()
        {
            this._view.PopulateProgramList(this.SearchProgram(this._view.TextBoxName));
        }

        public void SyncModelWithDatabase()
        {
            this._programList = Persistence.DataManager.Instance.SyncAcademicPrograms();
        }
    }
}
